using System.Collections.Generic;
using System.Data.SqlClient;

namespace DeVierKoeden.Dynamicweb.CreateSite
{
  internal class RefreshSettings : ISettings
  {
    public bool DisableScheduledTasks { get; set; }

    /// <summary>
    /// The name of the site in IIS (which is also used as the host header for the site).
    /// </summary>
    public string SiteName { get; set; }
    public string ApplicationPoolName { get; set; }

    public string ApplicationPoolAccount
    {
      get { return string.Format("IIS AppPool\\{0}", ApplicationPoolName); }
    }

    public string DwInstallRootFolder { get; set; }


    public bool MustCreateDatabase
    {
      get { return false; }
    }

    /// <summary>
    /// The folder where the selected ZIP file is extracted.
    /// </summary>
    public string ExtractedSourceFolder{ get; set; }

    /// <summary>
    /// The root folder for IIS. Can be a Dynamicweb application folder when installing a standard site, or the site's Application folder in case a custom site is set up.
    /// </summary>
    public string IisRootPath { get; set; }

    /// <summary>
    /// The path to the Admin folder that is used when the site is set up as a custom solution.
    /// </summary>
    public string IisAdminFolder { get; set; }

    /// <summary>
    /// The path to the SQL .bak file used for this site.
    /// </summary>
    public string DatabaseBackupPath { get; set; }

    /// <summary>
    /// The path to the data file (with default empty data) used for the /Files folder and database.
    /// </summary>
    public string DataFile { get; set; }

    /// <summary>
    /// The root folder of the site on disk.
    /// </summary>
    public string TargetFolder { get; set; }

    /// <summary>
    /// The /Application folder on disk when the site is set up as a custom solution.
    /// </summary>
    public string TargetApplicationFolder { get; set; }

    /// <summary>
    /// The /Files folder on disk.
    /// </summary>
    public string TargetFilesFolder { get; set; }

    /// <summary>
    /// The instance of SQL Server used by this site.
    /// </summary>
    public string SqlServerInstance { get; set; }

    /// <summary>
    /// The name of the database in SQL Server.
    /// </summary>
    public string DatabaseName { get; set; }

    /// <summary>
    /// The path to the physical .mdf SQL Server data file used for the new database.
    /// </summary>
    public string DatabasePhysicalName { get; set; }

    /// <summary>
    /// The path to the physical .ldf SQL Server log file used for the new database.
    /// </summary>
    public string DatabaseLogName { get; set; }

    /// <summary>
    /// Determines if this site uses its own Application folder.
    /// </summary>
    public bool ShouldRefreshApplication { get; set; }

    /// <summary>
    /// Determines if this site uses its own Files folder.
    /// </summary>
    public bool ShouldRefreshFilesFolder { get; set; }

    /// <summary>
    /// Determines if this site uses its own SQL Server database.
    /// </summary>
    public bool ShouldRefreshSqlDatabase { get; set; }

    /// <summary>
    /// The name of the custom SQL Server database.
    /// </summary>
    public string CustomSqlDatabase { get; set; }

    /// <summary>
    /// The physical path to a ZIP file with the custom sources.
    /// </summary>
    public string CustomZipSourceFile { get; set; }

    public bool ResetPassword { get; set; }
    public bool AnonymizeData { get; set; }

    public bool IsCustomSolution { get; set; }

    public SolutionType SolutionType { get; set; }

    public ReplacementBehavior AnonymizationMethod { get; set; }

    /// <summary>
    /// Validates the installation settings and checks the presence of the configured SQL Server instance.
    /// </summary>
    /// <returns></returns>
    internal IEnumerable<string> ValidateProperties()
    {
      if (string.IsNullOrEmpty(CustomZipSourceFile))
      {
        yield return "Please specify a zip file.";
      }

      bool hasSqlError = false;
      using (var sqlConnection = new SqlConnection($"data source={SqlServerInstance}; integrated security=true;"))
      {
        try
        {
          sqlConnection.Open();
        }
        catch
        {
          hasSqlError = true;
        }
        if (hasSqlError)
        {
          yield return
            $"The SQL server instance {SqlServerInstance} cannot be reached. Make sure the appSetting \"SqlServerInstance\" points to a valid instance of SQL Server.";
        }
      }
    }
    public string BuildConnectionString()
    {
      var builder = new SqlConnectionStringBuilder() { DataSource = SqlServerInstance, InitialCatalog = DatabaseName, IntegratedSecurity = true };
      return builder.ToString();
    }
  }
}
